package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dao.inspector;
import com.dao.outs;
import com.dao.trans_inspector;
import com.dao.wxuser;
import com.services.WxServices;
import com.utils.GlobalResult;
import com.utils.WechatUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@Transactional
public class InspectorController {
    @Autowired
    WxServices wxServices;
    @RequestMapping("/inspector_login")
    public GlobalResult wx_login_check(@RequestParam("code") String code,
                                       @RequestParam("rawData")String rawData,
                                       @RequestParam("signature")String signature,
                                       @RequestParam("encrypteData")String encrypteData,
                                       @RequestParam("iv")String iv, HttpServletRequest request)
    {
        JSONObject rawDataJson = JSON.parseObject(rawData);
        System.out.println("rowData:"+rawData);
        JSONObject session = WechatUtil.getSessionKeyOrOpenId(code);//提交数据给官网接口，返回数据
        String openid = session.getString("openid");
        String sessionKey = session.getString("session_key");
        String signature2 = DigestUtils.sha1Hex(rawData + sessionKey);
        if (!signature.equals(signature2)) {
            return GlobalResult.build(500, "签名校验失败", null);
        }

        wxuser wxuser = this.wxServices.getWxUseByOpenId(openid);
        String skey = UUID.randomUUID().toString();
        if(wxuser == null){
            String nickName = rawDataJson.getString("nickName");
            String avatarUrl = rawDataJson.getString("avatarUrl");
            String gender = rawDataJson.getString("gender");
            String city = rawDataJson.getString("city");
            String country = rawDataJson.getString("country");
            String province = rawDataJson.getString("province");
            wxuser = new wxuser();
            wxuser.setOpenid(openid);
            wxuser.setSkey(skey);
            wxuser.setSkey(sessionKey);
            wxuser.setCity(city);
            wxuser.setProvince(province);
            wxuser.setCountry(country);
            wxuser.setNickname(nickName);
            wxServices.insert(wxuser);
            System.out.println(wxuser);
        }else{
            wxuser.setSkey(skey);
        }
        inspector inspector  = wxServices.getInspector(openid);
        GlobalResult globalResult;
        if(inspector == null){
            trans_inspector trans1 = wxServices.getTrans(openid);
            System.out.println("未录入检查人员信息");
            if(trans1==null) {
                //未录入且未提交审核
                globalResult = new GlobalResult(201, "-1", openid);
            }else
            {
                //未录入但已提交审核
                globalResult = new GlobalResult(201,trans1.getChecked(), openid);
            }
        }
        else{
            //已录入，代表之前已审核通过
            globalResult = new GlobalResult(200,null,inspector);
        }
        return globalResult;
    }
    @RequestMapping("/check_out")
    public GlobalResult checkOut(@RequestParam("inspector_openid")String inspector_openid,
                                @RequestParam("Uopenid")String openid,
                                @RequestParam("id_number")String id_number,
                                @RequestParam("phone")String phone,
                                @RequestParam("room_id")String room_id,
                                @RequestParam("name")String name,
                                @RequestParam("year")String year,
                                @RequestParam("sex")String sex,
                                @RequestParam("health")String health,
                                @RequestParam("temp")String temp){
        System.out.println(inspector_openid+openid+id_number+phone+room_id+name+year+sex+health+temp);
        GlobalResult globalResult = new GlobalResult(200,null,"success");
        outs outs = new outs();
        outs.setInspector_openid(inspector_openid);
        outs.setUser_openid(openid);
        outs.setHealth(health);
        Date time = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        outs.setTimes(simpleDateFormat.format(time));
        outs.setTemp(temp);
        wxServices.insertIntoOuts(outs);
        return globalResult;
    }
    @RequestMapping("/check_in")
    public GlobalResult checkIn(@RequestParam("inspector_openid")String inspector_openid,
                                @RequestParam("Uopenid")String openid,
                                @RequestParam("id_number")String id_number,
                                @RequestParam("phone")String phone,
                                @RequestParam("room_id")String room_id,
                                @RequestParam("name")String name,
                                @RequestParam("year")String year,
                                @RequestParam("sex")String sex,
                                @RequestParam("health")String health,
                                @RequestParam("temp")String temp){
        System.out.println(inspector_openid+openid+id_number+phone+room_id+name+year+sex+health+temp);
        GlobalResult globalResult = new GlobalResult(200,null,"success");
        outs outs = new outs();
        outs.setInspector_openid(inspector_openid);
        outs.setUser_openid(openid);
        outs.setHealth(health);
        Date time = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        outs.setTimes(simpleDateFormat.format(time));
        outs.setTemp(temp);
        wxServices.insertIntoEnter(outs);
        return null;
    }
    @RequestMapping("/get_data_number")
    public int[] getNumber(){
        int a[] = new int[4];
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time = new Date();
        System.out.println(simpleDateFormat.format(time));
        a = wxServices.getData_Number(simpleDateFormat.format(time));
        return a;
    }
    @RequestMapping("/trans_inspector")
    public GlobalResult trans_inspector(
            @RequestParam("openid")String openid,
            @RequestParam("name")String name,
            @RequestParam("phone")String phone,
            @RequestParam("password")String password,
            @RequestParam("sex")String sex)
           {
               if(openid==null){
                   GlobalResult globalResult = new GlobalResult(500,null,"无openid");
                   return globalResult;
               }
               System.out.println(openid+name+phone+sex+password);
               trans_inspector trans = new trans_inspector();
               trans.setChecked("0");
               trans.setOpenid(openid);
               trans.setName(name);
               trans.setPhone(phone);
               trans.setPassword(password);
               trans.setSex(sex);
               wxServices.insertIntoTrans_Inspector(trans);
               GlobalResult globalResult = new GlobalResult(200,null,0);
               return globalResult;
    }


    @Test
    public void t()
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        simpleDateFormat.format(time);
        System.out.println(simpleDateFormat.format(time));
    }
}
